import pandas as pd
import pymysql

from myapp.elecmain import mysqlconnect

# 读取 XLSX 文件
file_path = r'C:\django\elecbabydjango\myapp\password\5位数25X3密码表765.xlsx'
data = pd.read_excel(file_path)

connect = mysqlconnect.mysql_connect()
try:
    with connect.cursor() as cursor:

        # 循环保存 A 类和 B 类密码
        for index, row in data.iterrows():
            a_passwords = row[1:26].tolist()  # A 类密码（第 1-25 列）
            b_passwords = row[26:51].tolist()  # B 类密码（第 26-50 列）
            c_passwords = row[51:76].tolist()  # C 类密码（第 50-75 列）
            reset_code = row.iloc[76]
            # print(a_passwords)
            for a_pass in a_passwords:

                sql = "INSERT INTO password (a_type) VALUES (%s);"
                cursor.execute(sql, (a_pass,))

            # for b_pass in b_passwords:
            #
            #     sql = "INSERT INTO password (b_type) VALUES (%s);"
            #     cursor.execute(sql, (b_pass,))
            #
            # for c_pass in c_passwords:
            #
            #     sql = "INSERT INTO password (c_type) VALUES (%s);"
            #     cursor.execute(sql, (c_pass,))
            #
            # for d_pass in reset_code:
            #
            #     sql = "INSERT INTO password (reset_code) VALUES (%s);"
            #     cursor.execute(sql, (d_pass,))
        # 提交更改
        connect.commit()

        print("密码已成功保存到数据库中。")
except Exception as e:
    print("出现错误:", e)
